package com.alquimia.identityaccess.rest.controller;

import com.alquimia.identityaccess.application.events.user.RequestUserEvent;
import com.alquimia.identityaccess.application.events.user.UserEvent;
import com.alquimia.identityaccess.application.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by Ivo on 14/07/2014.
 */
@Controller
@RequestMapping("/users")
public class UserQueriesController {

    private static Logger LOG = LoggerFactory.getLogger(UserQueriesController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET, value = "/{userId}")
    public ResponseEntity<UserEvent> user(@PathVariable String userId) {

        UserEvent userEvent = userService.user(new RequestUserEvent(userId));

        if (!userEvent.isEntityFound()) {
            return new ResponseEntity<UserEvent>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<UserEvent>(userEvent, HttpStatus.OK);
    }
}
